package com.thinkTank.oa.service.impl;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkTank.oa.base.BaseServiceImpl;
import com.thinkTank.oa.dao.PermissionMapper;
import com.thinkTank.oa.dao.RoleMapper;
import com.thinkTank.oa.entity.Role;
import com.thinkTank.oa.service.RoleService;
@Transactional
@Service("roleService")
public class RoleServiceImpl extends BaseServiceImpl<Role> implements  RoleService  {
	public RoleMapper getRoleMapper() {
		return (RoleMapper)super.getBaseMapper();
	}
	@Autowired
	private PermissionMapper permissionMapper;
	@Autowired	
	public void setRoleMapper(RoleMapper roleMapper) {
		super.setBaseMapper(roleMapper);
	}
	public Integer addRoleAndPermission(Role role,String menu,String permi){
		this.getRoleMapper().add(role);
		if(role.getPid()>0){
			if(menu!=null&&!"".equals(menu)){
				String menuArra []=menu.split(",");
				for(String str:menuArra){
			
					permissionMapper.addRoleAndPermission(role.getPid(), Integer.parseInt(str));
				}
			}
			if(permi!=null&&!"".equals(permi)){
				String permiArra []=permi.split(",");
				for(String str:permiArra){
					
					permissionMapper.addRoleAndPermission(role.getPid(), Integer.parseInt(str));
				}
			}
			
			
		}
		return role.getPid();
	}
	@Override
	public Integer updateRoleAndPerMission(Role role, String menu, String permi) {
		this.getRoleMapper().update(role);
		//先删除和角色相关的 权限
		permissionMapper.deletePermissionByRole(role.getPid());
		
		if(menu!=null&&!"".equals(menu)){
			String menuArra []=menu.split(",");
			for(String str:menuArra){
		
				permissionMapper.addRoleAndPermission(role.getPid(), Integer.parseInt(str));
			}
		}
		if(permi!=null&&!"".equals(permi)){
			String permiArra []=permi.split(",");
			for(String str:permiArra){
				
				permissionMapper.addRoleAndPermission(role.getPid(), Integer.parseInt(str));
			}
		}
		return role.getPid();
	}
}
